const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
Page({
  data: {
    inputValue:'',
    lists:[],
    page:1,
    pageSize:10,
    isEnd:false,
    isloading:false,
    type:1,
    filter:1,
    filter2:1,
  },
  onLoad: function (options) {
    // if(options.search){
    //   this.setData({
    //     inputValue:options.search
    //   })
    // }
    // this.search()
  },
  tabs(e){
    this.setData({
      type:e.currentTarget.dataset.type,
    })
  },
  changefilter(e){
    var name=e.currentTarget.dataset.filter;
    if(name=='iscondition'){
      this.setData({
        iscondition:!this.data.iscondition,
        isbrand:false,
        isprice:false,
        isfilter:false,
      })

    }
    if(name=='isbrand'){
      this.setData({
        iscondition:false,
        isbrand:!this.data.isbrand,
        isprice:false,
        isfilter:false,
      })
    }
    if(name=='isprice'){
      this.setData({
        iscondition:false,
        isbrand:false,
        pricesort:this.data.pricesort==0?1:this.data.pricesort==1?2:0,
        isprice:true,
        isfilter:false,
        page:1,
        pageSize:10,
        isEnd:false,
        goodsdata:[],
      })
      this.goodsList()
    }
    if(name=='isfilter'){
      this.setData({
        iscondition:false,
        isbrand:false,
        isprice:false,
        isfilter:!this.data.isfilter,
      })
    }
  },
  search(){
    var data={
      keyword:this.data.inputValue,
      isHot:'',
      page:this.data.page,
      pageSize:this.data.pageSize,
      companyId:'',
      city:''
    }
    Controller.joblist(data).then(res=>{
      if(res.data.baseData && res.data.baseData.list.length>0){
        this.setData({
          lists:this.data.lists.concat(res.data.baseData.list),
        })
      }
      if(res.data.baseData.lastPage){
        this.setData({
          isEnd:res.data.baseData.lastPage
        })
      }
      this.setData({
        isloading:false
      })
    })

  },
  cancel(){
    this.setData({
      inputValue:'',
      lists:[],
      page:1,
      pageSize:10,
      isEnd:false,
      isloading:true
    })
    this.search()
  },
  golink(e){
    // if(!wx.getStorageSync('logintoken')){
    //   app.ToLogin()
    //   return
    // }
    wx.navigateTo({
      url:e.currentTarget.dataset.url
    });
  },
  searchSubmit(e){
    console.log(e)
    let _SearchValue = this.data.inputValue;
    let localStorageValue = [];

    if (_SearchValue != '') {

        //添加搜索历史记录
        //调用API从本地缓存中获取数据
        localStorageValue = wx.getStorageSync('searchData') || [];

        let tempSearchData = [];
        //过滤历史列表中相同的搜索记录
        for (let item of localStorageValue) {
            if (item != _SearchValue) {
                tempSearchData.push(item);
            }
        }
        //添加搜索文本进历史记录
        tempSearchData.push(_SearchValue);
        //保存至从本地缓存
        wx.setStorageSync('searchData', tempSearchData);
        
    }
    this.setData({
      lists:[],
      page:1,
      pageSize:10,
      isEnd:false,
      isloading:true
    })
    this.search()
  },
  bindInput(e){
    var value=e.detail.value || ''

      this.setData({
        inputValue: value
      })
 
  },
  onShareAppMessage: function () {

  }
})